package com.example;

import java.util.ArrayList;
import java.util.List;

// 抽象类Component
abstract class Component {
    protected String name;

    public Component(String name) {
        this.name = name;
    }

    // 新增操作，对于订单无实际意义，在菜单中实现
    public void add(Component component) {
        throw new UnsupportedOperationException();
    }

    // 删除操作，对于订单无实际意义，在菜单中实现
    public void remove(Component component) {
        throw new UnsupportedOperationException();
    }

    // 查询操作
    public abstract void display();

    // 下单操作，对于菜单无实际意义，在订单中实现
    public void placeOrder() {
        throw new UnsupportedOperationException();
    }

    // 撤销操作，对于菜单无实际意义，在订单中实现
    public void cancelOrder() {
        throw new UnsupportedOperationException();
    }
}

// 订单类，叶子节点
class Order extends Component {
    public Order(String name) {
        super(name);
    }

    @Override
    public void display() {
        System.out.println("Order: " + name);
    }

    @Override
    public void placeOrder() {
        System.out.println("Placing order: " + name);
    }

    @Override
    public void cancelOrder() {
        System.out.println("Canceling order: " + name);
    }
}

// 菜单类，容器类，分支节点
class Menu extends Component {
    private List<Component> components = new ArrayList<>();

    public Menu(String name) {
        super(name);
    }

    @Override
    public void add(Component component) {
        components.add(component);
    }

    @Override
    public void remove(Component component) {
        components.remove(component);
    }

    @Override
    public void display() {
        System.out.println("Menu: " + name);
        for (Component component : components) {
            component.display();
        }
    }

    // 遍历子菜单
    public void traverseMenu() {
        for (Component component : components) {
            component.display();
            if (component instanceof Menu) {
                ((Menu) component).traverseMenu();
            }
        }
    }
}

